/**
 * @author deming.su
 * @description 定义axios和封装
 */
import axios, { InternalAxiosRequestConfig } from 'axios'
import { useCommonStore } from '../store'

const Axios = axios.create({
    baseURL: '/api'
})

// const Axios = axios.create({
//     baseURL: 'http://www.shuiyue.info:12600'
//     baseURL: 'http://10.7.162.59:12280'
// })

// Axios.interceptors.request.use(function(config: InternalAxiosRequestConfig): any {
//     config.headers.token = useCommonStore().tokenVal
//     return config
// })
Axios.interceptors.request.use(function(config: InternalAxiosRequestConfig): any {
    config.headers.token = 'U2FsdGVkX1+caJzGG76X+G8PKnk19xHqXcXTyb51lQZfFx2eQxj2R/wen8viASTytiL0/LNWKnuQspSD+m/k4T07+mL+NnArQj1Gs0k1/y8='
    return config
})

/* 封装为一个支持async + await的Promise方法 */
function Ajax(req: any): Promise<any> {
    return new Promise(resolve => {
        Axios.request({
            method: req.method || 'GET',
            data: req.data || {},
            params: req.params || {},
            url: req.url
        }).then(function({ data }) {
            resolve(data)
        }).catch(e => {
            resolve(e)
        })
    })
}

export default Ajax